package cn.xeblog.server.forward;

import cn.xeblog.server.config.ServerConfig;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author eleven
 * @date 2024/12/16 9:51
 * @apiNote
 */
@Slf4j
public class ForwardClient {
    public static void start() {
        ServerConfig config = ServerConfig.getConfig();
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            log.info("Forward Client started");
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ForwardClientInitializer());
            ChannelFuture future = bootstrap.connect(config.getForwardHost(), config.getForwardPort()).sync();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            group.shutdownGracefully();
        }
    }
}
